package com.sharemedia.views
{
	import com.sharemedia.consts.AppConst;
	import com.sharemedia.events.PrefEvent;
	import com.sharemedia.skins.defaultSkin.PrefDialogSkin;
	import com.sharemedia.utils.SkinUtil;
	
	import flash.display.DisplayObject;
	import flash.events.MouseEvent;
	
	import mx.core.FlexGlobals;
	import mx.core.UIComponent;
	import mx.managers.PopUpManager;
	
	import spark.components.Button;
	import spark.components.NumericStepper;
	import spark.components.TextInput;
	import spark.components.supportClasses.SkinnableComponent;
	import spark.events.TextOperationEvent;
	
	[Event(name="save", type="com.sharemedia.events.PrefEvent")]
	public class PrefDialog extends SkinnableComponent
	{
		SkinUtil.setSkin("com.sharemedia.views.PrefDialog", PrefDialogSkin);
		
		public function PrefDialog()
		{
			super();
		}
		
		[SkinPart]
		public var serverNameInput:TextInput;
		
		[SkinPart]
		public var bufferSizeStepper:NumericStepper;
		
		[SkinPart]
		public var saveButton:Button;
		
		[SkinPart]
		public var closeButton:Button;
		
		private var _serverName:String;

		public function get serverName():String
		{
			return _serverName;
		}

		public function set serverName(value:String):void
		{
			_serverName = value;
			
			if(serverNameInput)
				serverNameInput.text = serverName;
		}
		
		private var _bufferSize:Number=0; 

		public function get bufferSize():Number
		{
			return _bufferSize;
		}

		public function set bufferSize(value:Number):void
		{
			_bufferSize = value;
			
			if(bufferSizeStepper)
				bufferSizeStepper.value = value*AppConst.SOUND_CHUNK_SIZE/1024;
		}

		
		override protected function partAdded(partName:String, instance:Object):void
		{
			super.partAdded(partName, instance);
			
			switch(instance)
			{
				case serverNameInput:
				{
					serverNameInput.text = serverName;
					break;
				}
					
				case bufferSizeStepper:
				{
					bufferSizeStepper.value = bufferSize*AppConst.SOUND_CHUNK_SIZE/1024;
					break;
				}
					
				case saveButton:
				{
					saveButton.addEventListener(MouseEvent.CLICK, saveHandler);
					break;
				}
				case closeButton:
				{
					closeButton.addEventListener(MouseEvent.CLICK, closeHandler);
					break;
				}
			}
		}
		
		override protected function partRemoved(partName:String, instance:Object):void
		{
			super.partRemoved(partName, instance);
			
			switch(instance)
			{
				case saveButton:
				{
					saveButton.removeEventListener(MouseEvent.CLICK, saveHandler);
					break;
				}
				case closeButton:
				{
					closeButton.removeEventListener(MouseEvent.CLICK, closeHandler);
					break;
				}
			}
		}
		
		protected function saveHandler(event:MouseEvent):void
		{
			if(bufferSizeStepper)
				bufferSize = bufferSizeStepper.value*1024/AppConst.SOUND_CHUNK_SIZE;
			if(serverNameInput)
				serverName = serverNameInput.text;
			
			dispatchEvent(new PrefEvent(PrefEvent.SAVE, serverName, bufferSize));
			hide();
		}
		
		protected function closeHandler(event:MouseEvent):void
		{
			hide();
		}
		
		
		public function show(parent:DisplayObject=null):void
		{
			if(!parent)
				parent = FlexGlobals.topLevelApplication as DisplayObject;
			PopUpManager.addPopUp(this, parent, true);
			PopUpManager.centerPopUp(this);
		}
		
		public function hide():void
		{
			PopUpManager.removePopUp(this)
		}
	}
}